home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 5152 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  1.8 KB

  1. Path: inforamp.net!ts44-07
  2. From: rmorin@inforamp.net (Randy Charles Morin)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Borland C++ 4.5 : delete [] operator - what's going on here?
  5. Date: Fri, 02 Feb 96 19:11:19 GMT
  6. Organization: MiddleWorld SoftWare
  7. Message-ID: <4etnap$7s1@sam.inforamp.net>
  8. References: <4espam$ddf@rks1.urz.tu-dresden.de>
  9. NNTP-Posting-Host: ts44-07.tor.inforamp.net
  10. X-Newsreader: News Xpress Version 1.0 Beta #4
  11.  
  12. In article <4espam$ddf@rks1.urz.tu-dresden.de>,
  13.    Hoang Minh Son <hoang@eatns1.et.tu-dresden.de> wrote:
  14. >HI,
  15. >I'm using EasyWin (Borland C++ 4.5) to test my matrix library under Windows 
  16. 3.1. 
  17. >Could anybody tell me what's wrong in the following code?
  18. >
  19. >template<class T> class TMatrix
  20. >{  
  21. >  public:    
  22. >        TMatrix(size_t m = 0, size_t n = 0);
  23. >    ~TMatrix()    
  24. >                {
  25. >        delete[] elem;
  26. >        delete[] pcol;
  27. >            }
  28. >        T& operator()(size_t i, size_t j);
  29. >        {
  30. >        return pcol[j][i];
  31. >        }
  32. >
  33. >          //....
  34. >  private:
  35. >        size_t nrow;
  36. >        size_t ncol;
  37. >        T**    pcol;           // pointer to columns
  38. >        T*     elem;           // element array
  39. >};
  40. >
  41. >template<class T>
  42. >TMatrix<T>::TMatrix(size_e m, size_t n) : nrow(m), ncol(n)
  43. >{    
  44. >        elem = new T[m*n+1];  //  a dummy space for efficient use 
  45. >        pcol = new T*[n+1];   //  of 1-based indexing
  46. >            
  47. >    if (n > 0)
  48. >    {
  49. >        pcol[1] = elem;
  50. >        for (int i=1; i <= n; i++)
  51. >            pcol[i+1] = pcol[i] + m;
  52. >    }
  53. >}
  54. >
  55. >typedef TMatrix<double> Matrix; 
  56. >
  57. >void test()
  58. >{
  59. >    Matrix a(4,4);
  60. >    for (int i = 1; i <= 4; i++)
  61. >        for (int j = 1; j <= 4; j++)
  62. >            a(i,j) = i + j;
  63. >}
  64. >
  65.  
  66. The four elements of an array 
  67.     int a[4];
  68. are
  69.     a[0], a[1], a[2], a[3]
  70.  
  71. In your example you use
  72.     a[1], a[2], a[3], a[4]
  73. and compensate by adding a cell and column.
  74.  
  75. This is likely your problem.
  76. This is very unconventional.
  77.  
  78. Agrivar
  79.